package com.dili.dd.gpi.controller;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.dili.dd.gpi.domain.SiteApp;
import com.dili.dd.gpi.enums.APPType;
import com.dili.dd.gpi.service.SiteAppService;
import com.dili.dd.gpi.util.spring.HttpParameterParser;
import com.dili.dd.gpi.util.spring.JsonView;
import com.dili.dd.gpi.util.spring.ObjectConvertAnno;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by zhaoYuan on 2015/10/20.
 */
@Controller
@RequestMapping(value = "/sapp")
public class SiteAppController {

    @Resource
    private SiteAppService siteAppService;

    @RequestMapping(value = "/save.htm",method = RequestMethod.PUT)
    public ModelAndView add(int siteId,@ObjectConvertAnno HttpParameterParser parser){
        try {
            String[] webs = parser.getStringArray("web");
            String[] apps = parser.getStringArray("app");
            List<SiteApp> sas = new LinkedList<>();
            if(webs != null && webs.length >0){
                for (int i = 0; i < webs.length; i++) {
                    String web = webs[i];
                    SiteApp sa = new SiteApp();
                    sa.setSiteId(siteId);
                    sa.setKey(web);
                    sa.setAppType(APPType.WEB);
                    sas.add(sa);
                }
            }
            if(apps != null && apps.length >0){
                for (int i = 0; i < apps.length; i++) {
                    String app = apps[i];
                    SiteApp sa = new SiteApp();
                    sa.setSiteId(siteId);
                    String[] split = app.split(",");
                    sa.setKey(split[0]);
                    sa.setAppType(APPType.valueOf(split[1]));
                    sas.add(sa);
                }
            }
            siteAppService.batAddSiteApp(sas);
            return new ModelAndView(new JsonView(true,"添加成功"));
        } catch (Exception e) {
            return new ModelAndView(new JsonView(false,e.getMessage()));
        }
    }

    @RequestMapping(value = "/{id}/",method = RequestMethod.DELETE)
    public ModelAndView delete(@PathVariable int id){
        try {
            siteAppService.deletedSiteApp(id);
            return new ModelAndView(new JsonView(true,"success"));
        } catch (Exception e) {
            return new ModelAndView(new JsonView(false,e.getMessage()));
        }
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ModelAndView list(int siteId){
        List<SiteApp> sites = siteAppService.findBySite(siteId);
        return new ModelAndView(new JsonView(sites, SerializerFeature.WriteMapNullValue));
    }

}
